package com.andatsoft.laisim.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.os.Handler;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.andatsoft.laisim.R;

public class SlideView extends View {

	private int layout_width = 720;// the width of the view
	private int layout_height = 50;// the height of the view
	private int mThumbWidth;
	private int mEndTextLeft;// the left of the end text (in pixel)
	private int mBackgroundColor = 0xff9988ff;
	private int mThumbColor = 0xff9988ff;
	private Rect mRectThumb;
	private Rect mRectBackground;

	private Paint mThumbPainter;
	private Paint mBackgroundPainter;
	private Paint mTextPainter;
	private Paint mEndTextPainter;
	private Context mContext;

	/**
	 * @author Paul Call back when slide move to the end
	 * 
	 */
	public interface OnSlideChangedListener {
		public void onSlideEnded();
	}

	private OnSlideChangedListener mOnSlideChanged;

	public SlideView(Context context) {
		super(context);
		this.mContext = context;
	}

	public SlideView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		layout_width = w;
		mThumbWidth = layout_width / 5;
		mEndTextLeft = layout_width - 80;
		layout_height = h;
		mRectThumb = new Rect(0, 0, mThumbWidth, layout_height);
		mRectBackground = new Rect(0, 0, layout_width, layout_height);
		setupPainters();
	}

	private void setupPainters() {
		if (mBackgroundPainter == null)
			mBackgroundPainter = new Paint();
		mBackgroundPainter.setColor(mBackgroundColor);
		mBackgroundPainter.setAntiAlias(true);
		mBackgroundPainter.setStyle(Style.STROKE);
		mBackgroundPainter.setStrokeWidth(2f);
		mBackgroundPainter.setAlpha(50);

		if (mThumbPainter == null)
			mThumbPainter = new Paint();
		mThumbPainter.setColor(mThumbColor);
		mThumbPainter.setAntiAlias(true);
		mThumbPainter.setStyle(Style.FILL);
		mThumbPainter.setAlpha(50);
		if (mTextPainter == null)
			mTextPainter = new TextPaint();
		mTextPainter.setColor(0xffffffff);
		mTextPainter.setAlpha(50);
		mTextPainter.setTextSize(25f);
		if (mEndTextPainter == null)
			mEndTextPainter = new TextPaint();
		mEndTextPainter.setColor(0xffffffff);
		mEndTextPainter.setAlpha(50);
		mEndTextPainter.setTextSize(40f);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// draw background
		canvas.drawRect(mRectBackground, mBackgroundPainter);
		// draw thumb
		canvas.drawRect(mRectThumb, mThumbPainter);
		// draw string
		String text = mContext.getString(R.string.common_text_slide_to_unlock);
		canvas.drawText(text, layout_width / 2 - mTextPainter.measureText(text)
				/ 2, layout_height / 2 + 10, mTextPainter);
		// end text string
		canvas.drawText(">> ", mEndTextLeft, layout_height / 2 + 10,
				mEndTextPainter);
	}

	private Handler mPullSlideHandler = new Handler();
	private Runnable pullSlideBack = new Runnable() {

		@Override
		public void run() {
			mRectThumb.right -= 10;
			if (mRectThumb.right < mThumbWidth) {
				mRectThumb.right = mThumbWidth;
				mPullSlideHandler.removeCallbacks(pullSlideBack);
			} else {
				mPullSlideHandler.post(pullSlideBack);
			}
			invalidate();
		}
	};

	private boolean mIsSlideEnded;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mBackgroundPainter.setAlpha(100);
			mThumbPainter.setAlpha(100);
			invalidate();
			break;
		case MotionEvent.ACTION_MOVE:
			mRectThumb.right = (int) event.getX();
			if (mRectThumb.right < mThumbWidth)
				mRectThumb.right = mThumbWidth;
			mEndTextPainter.setAlpha(100);
			if (mRectThumb.right > mEndTextLeft) {
				mRectThumb.right = layout_width;
				mIsSlideEnded = true;
				if (mOnSlideChanged != null) {
					mOnSlideChanged.onSlideEnded();
				}
			}
			invalidate();
			break;
		case MotionEvent.ACTION_UP:
			mBackgroundPainter.setAlpha(50);
			mThumbPainter.setAlpha(50);
			mEndTextPainter.setAlpha(50);
			if (!mIsSlideEnded) {
				mPullSlideHandler.post(pullSlideBack);
			}
			invalidate();
			break;

		default:
			break;
		}
		return true;
	}

	public void setOnSlideChanged(OnSlideChangedListener event) {
		this.mOnSlideChanged = event;
	}
}
